package com.dt76.smallloanbackendservice.service.etao.impl;

import com.dt76.smallloanbackendservice.mapper.etao.DeparmentMapper;
import com.dt76.smallloanbackendservice.pojo.etao.CurrentUser;
import com.dt76.smallloanbackendservice.pojo.etao.Department;
import com.dt76.smallloanbackendservice.service.etao.DepartmentService;
import com.dt76.smallloanbackendservice.utils.DateUtils;
import com.dt76.smallloanbackendservice.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    DeparmentMapper deparmentMapper;

    @Override
    public Page getAllDept(Integer pageIndex) {
        Page page = new Page();
        page.setPageIndex(pageIndex);
        page.setTotalCount(deparmentMapper.getTotalCount());
        List<Department> allDept = deparmentMapper.getAllDept((page.getPageIndex() - 1) * page.getPageSize(), page.getPageSize());
        for (Department dept : allDept) {
            dept.setCreateTimeStr(DateUtils.dateToString(dept.getCreateTime(), "yyyy-MM-dd"));
        }
        page.setRows(allDept);
        return page;
    }

    @Override
    public Department getDetpById(Integer did) {
        return deparmentMapper.getDetpById(did);
    }

    @Override
    public List<Department> getDept() {
        return deparmentMapper.getDept();
    }

    @Override
    public boolean addNewDept(Department department,Integer currentId) {
        boolean fg = false;
        if (deparmentMapper.saveDept(department) == 1) {
            fg = true;
        }
        return fg;
    }

    @Override
    public boolean updDept(Department department,Integer currentId) {
        boolean fg = false;
        int i = deparmentMapper.updateDept(department);
        if (i == 1) {
            fg = true;
        }
        return fg;
    }

    @Override
    public boolean updDeptEnable(Integer did, Integer enable) {
        boolean fg = false;
        if (deparmentMapper.updateDeptEnable(did,enable) == 1) {
            fg = true;
        }
        return fg;
    }

    @Override
    public boolean delDeptById(Integer did, CurrentUser currentUser) {
        boolean fg = false;
        if (deparmentMapper.deleteDeptById(did) == 1) {
            fg = true;
        }
        return fg;
    }
}
